package com.boc.ewr.uniapp.api.service.impl;

import com.alibaba.druid.util.DaemonThreadFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.Cac;
import com.boc.ewr.bcimp.entity.CassTlrMsg;
import com.boc.ewr.bcimp.service.ICacService;
import com.boc.ewr.bcimp.service.ICassTlrMsgService;
import com.boc.ewr.business.entity.*;
import com.boc.ewr.business.service.*;
import com.boc.ewr.uniapp.api.service.ICaDetailService;
import com.boc.ewr.uniapp.api.vo.CaUncaDtlRespVo;
import com.boc.ewr.uniapp.api.vo.CaUncaDtlVo;
import com.boc.ewr.work.entity.WorkRecordDtl;
import com.boc.ewr.work.service.IWorkRecordDtlService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author : Wzz
 * @create : 2023-05- 23- 17:15
 * @desc :
 */
@Service
public class CaDetailServiceImpl implements ICaDetailService {

    @Autowired
    private IAcctDtlClassA1Service acctDtlClassA1Service;

    @Autowired
    private IAcctDtlClassA2Service acctDtlClassA2Service;

    @Autowired
    private IAcctDtlClassA3Service acctDtlClassA3Service;

    @Autowired
    private IAcctDtlClassCService acctDtlClassCService;

    @Autowired
    private IAcctDtlClassDService acctDtlClassDService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private ICacService cacService;

    @Autowired
    private IWorkRecordDtlService workRecordDtlService; //工作记录明细


    @Override
    public List<CaUncaDtlVo> getCaDetailByType(String acctType, String likeName) {

        String orgCode = this.getLoginUser().getOrgCode();
        List<CaUncaDtlVo> resultList = new ArrayList<>();
        if (acctType.equals("A1")) {
            LambdaQueryWrapper<AcctDtlClassA1> wrapper =
                    new LambdaQueryWrapper<AcctDtlClassA1>()
                            .eq(AcctDtlClassA1::getBranchNo, orgCode);
            if (!"".equals(likeName)) {
                wrapper.like(AcctDtlClassA1::getCusmNm, likeName);
            }
            List<AcctDtlClassA1> list = acctDtlClassA1Service.list(wrapper);
            for (AcctDtlClassA1 acctDtlClassA1 : list) {

                CaUncaDtlVo caUncaDtlVo = new CaUncaDtlVo();
                BeanUtils.copyProperties(acctDtlClassA1, caUncaDtlVo);
                caUncaDtlVo.setAcctType("A1");
//                caUncaDtlVo.setCaPeriodNo("xxx");
                resultList.add(caUncaDtlVo);
            }

        } else if (acctType.equals("A2")) {
            LambdaQueryWrapper<AcctDtlClassA2> wrapper =
                    new LambdaQueryWrapper<AcctDtlClassA2>()
                            .eq(AcctDtlClassA2::getBranchNo, orgCode);
            if (!"".equals(likeName)) {
                wrapper.like(AcctDtlClassA2::getCusmNm, likeName);
            }
            List<AcctDtlClassA2> list = acctDtlClassA2Service.list(wrapper);
            for (AcctDtlClassA2 acctDtlClassA2 : list) {
                CaUncaDtlVo caUncaDtlVo = new CaUncaDtlVo();
                BeanUtils.copyProperties(acctDtlClassA2, caUncaDtlVo);
                caUncaDtlVo.setAcctType("A2");
//                caUncaDtlVo.setCaPeriodNo("xxx");
                resultList.add(caUncaDtlVo);
            }

        } else if (acctType.equals("A3")) {
            LambdaQueryWrapper<AcctDtlClassA3> wrapper =
                    new LambdaQueryWrapper<AcctDtlClassA3>()
                            .eq(AcctDtlClassA3::getBranchNo, orgCode);
            if (!"".equals(likeName)) {
                wrapper.like(AcctDtlClassA3::getCusmNm, likeName);
            }
            List<AcctDtlClassA3> list = acctDtlClassA3Service.list(wrapper);
            for (AcctDtlClassA3 acctDtlClassA3 : list) {
                CaUncaDtlVo caUncaDtlVo = new CaUncaDtlVo();
                BeanUtils.copyProperties(acctDtlClassA3, caUncaDtlVo);
                caUncaDtlVo.setAcctType("A3");
//                caUncaDtlVo.setCaPeriodNo("xxx");
                resultList.add(caUncaDtlVo);
            }
        } else if (acctType.equals("C")) {
            LambdaQueryWrapper<AcctDtlClassC> wrapper =
                    new LambdaQueryWrapper<AcctDtlClassC>()
                            .eq(AcctDtlClassC::getBranchNo, orgCode);
            if (!"".equals(likeName)) {
                wrapper.like(AcctDtlClassC::getCusmNm, likeName);
            }
            List<AcctDtlClassC> list = acctDtlClassCService.list(wrapper);
            for (AcctDtlClassC acctDtlClassC : list) {
                CaUncaDtlVo caUncaDtlVo = new CaUncaDtlVo();
                BeanUtils.copyProperties(acctDtlClassC, caUncaDtlVo);
                caUncaDtlVo.setAcctType("C");
//                caUncaDtlVo.setCaPeriodNo("xxx");
                resultList.add(caUncaDtlVo);
            }

        }

        return resultList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addWorkRecord(CaUncaDtlRespVo caUncaDtlRespVo) {
        // 更新工作记录
        WorkRecordDtl workRecordDtl = new WorkRecordDtl();
        BeanUtils.copyProperties(caUncaDtlRespVo, workRecordDtl);

        LoginUser loginUser = this.getLoginUser();
        String orgCode = loginUser.getOrgCode();
        workRecordDtl.setParentBranchNo(sysBaseAPI.getParentDepartId(orgCode).getText());
        workRecordDtl.setOrgNo(orgCode);
        workRecordDtl.setOrgNm(sysBaseAPI.selectAllById(orgCode).getDepartName());
        workRecordDtl.setEmpNm(loginUser.getRealname());

        // 同对账单号和同协议号下存在不同币种对应的账号，此处不涉及账号，因此可直接取第一个
        Cac cac = cacService.list(
                new LambdaQueryWrapper<Cac>()
                        .eq(Cac::getCaNo, workRecordDtl.getCaNo())
                        .eq(Cac::getCaProtocolNo, workRecordDtl.getProtocolNo())
        ).get(0);

        workRecordDtl.setCusmAddr(cac.getAccSendAddr());
        workRecordDtl.setContactNm(cac.getLinkerName());
        workRecordDtl.setContactMob(cac.getLinkerMobile());
        workRecordDtl.setCommunicateTime(new Date());

        // ehr号就是工号
        String workNoByLoginUser = sysBaseAPI.getWorkNoByLoginUser(this.getLoginUser());
        workRecordDtl.setEhrTlrNo(workNoByLoginUser);

        workRecordDtlService.save(workRecordDtl);
    }


    // 获取登录角色部门code
    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }

}
